<template>
    <div>
        <el-page-header @back="gotoback" title="返回">
            <template #content>
                <el-breadcrumb-item :to="{ path: '/organization/user' }">用户列表</el-breadcrumb-item>
            </template>
        </el-page-header>
    </div>
    <el-skeleton :rows="8" :loading="load" animated>
        <div class="header">
            <div class="head">
                <el-row>
                    <el-col :span="8">
                        <el-button type="primary" style="margin-top: 1vw;margin-right: 20vw;"
                            @click="useradd">新增</el-button>
                    </el-col>
                    <el-col :span="8">
                        <el-input placeholder="输入姓名或账号进行查询" v-model="input3" @input="btnselect"
                            style="margin-top: 1vw;">
                            <template #append>
                                <el-button>查询</el-button>
                            </template>
                        </el-input>
                    </el-col>
                    <el-col :span="8">
                        <div style="margin-top: 2vh;">
                            <el-checkbox @change="(val) => checkbox(val, item)" v-for="(item, index) in detailsList"
                                :label="item.title" :key="index">
                                {{ item.title }}
                            </el-checkbox>
                        </div>
                    </el-col>
                </el-row>
            </div>
            <div class="main">
                <el-table v-loading="loading" :data="tableData" border class="table" max-height="55vh">
                    <el-table-column fixed prop="id" label="序号" type="index" width="80">
                        <template #default="score">
                            {{ (page - 1) * limit + score.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="100" />
                    <el-table-column prop="account" label="账号" />
                    <el-table-column prop="organizations" label="组织/职务">
                        <template #default="scope">
                            <el-tag :key="children.id" v-for="children in scope.row.organizations">{{ children.oname }}-{{
                                children.zname
                            }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isActive" label="激活" width="80">
                        <template #default="scope">
                            <el-tag :type="scope.row.isActive == 1 ? 'success' : 'danger'">
                                {{ scope.row.isActive == 1 ? '是' : '否' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isBan" label="禁用" width="80">
                        <template #default="scope">
                            <el-tag type="success" v-if="scope.row.isBan == 1">是</el-tag>
                            <el-tag v-else type="warning">否</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="添加时间" />
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button size="small" type="danger" v-if="scope.row.isActive == 0 && scope.row.isBan == 0"
                                @click="details(scope.row)">删除</el-button>
                            <el-button size="small" type="success" v-else
                                v-show="scope.row.isActive == 1 && scope.row.isBan == 1" plain
                                @click="des(scope.row)">启用</el-button>
                            <el-button size="small" type="danger" v-show="scope.row.isActive == 1 && scope.row.isBan == 0"
                                plain style="margin-left: -1px;" @click="del(scope.row)">禁用</el-button>
                            <el-button size="small" type="primary" @click="detail(scope.row)">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="example-pagination-block" style="margin-top: 1vh;">
                    <el-pagination @current-change="changes" :page-size="limit" :current-page="page"
                        layout="prev, pager, next" :total="total" />
                </div>
            </div>
        </div>
    </el-skeleton>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted } from 'vue'
import { GetUsers, BanUsers, DeleteUsers } from '../../../api/user/user'//显示数据·启用禁用·用户删除
import type { TypeuserDto } from '../../../type/user'
import { useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { ElMessage } from 'element-plus'

export default defineComponent({

    setup() {
        const router = useRouter();
        const data = reactive<TypeuserDto>({
            input3: '',
            IsB: '',//不看禁用
            IsA: '',//只看激活
            Search: '',//输入框图标
            radio: '',//单选框
            tableData: [],//数据
            limit: 10,//每页显示条数
            page: 1,//当前页
            total: 0,//总条数
            loading: false,//加载
            load: true,//骨架屏
            key: '',//值
            isBan: "",//是否禁用
            detailsList: [
                {
                    "title": "不看禁用",
                },
                {
                    "title": "只看激活",
                }
            ],
            isActive: ""//是否激活
        })
        const events = {
            //点击page触发事件
            changes(val: any) {
                data.loading = true
                data.page = val
                events.data(data.key, data.isBan, data.isActive)
            },

            //显示刷新数据
            data(key: any, isBan: any, isActive: any) {
                GetUsers({
                    page: data.page,
                    limit: data.limit,
                    key: data.input3,
                    isActive: isActive,
                    isBan: isBan
                }).then((res: any) => {
                    console.log('用户数据', res)
                    data.tableData = res.data.data
                    data.loading = false
                    data.load = false
                    data.total = res.data.total
                })
            },
            //返回上一级
            gotoback() {
                window.history.go(-1)
            },
            //跳转新增页面
            useradd() {
                router.push("/organization/user/add")
            },
            //跳转详情页面
            detail(val: any) {
                console.log("详情id", val.id)
                router.push({ path: `/organization/user/detail/${val.id}` })
            },
            //删除
            details(obj: any) {
                ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    data.loading = true
                    DeleteUsers({
                        ids: [obj.id]
                    }).then((res: any) => {
                        if (res.result === 0) {
                            ElMessage({
                                message: '删除成功',
                                type: 'success',
                            })
                            events.data(data.key, data.isBan, data.isActive)
                        } else {
                            ElMessage.error({
                                message: '删除失败',
                                type: 'error',
                            })
                        }
                        data.loading = false
                    })
                }).catch(() => {
                    ElMessage.info("已取消删除")
                })

            },
            //查询
            btnselect() {
                data.loading = true
                events.data(data.input3)
            },
            //不看禁用
            isB() {
                data.loading = true
                console.log('8888')
                console.log(data.IsB, "sss")
                if (data.IsB) {
                    data.isBan = 0;
                    events.data(data.key, data.isBan, data.isActive);
                } else {
                    data.isBan = "";
                    events.data(data.key, data.isBan, data.isActive);
                }
                data.loading = false
            },
            //只看激活
            isA() {
                data.loading = true
                console.log('6666')
                if (data.IsA) {
                    data.isActive = 1;
                    events.data(data.key, data.isBan, data.isActive);
                } else {
                    data.isActive = "";
                    events.data(data.key, data.isBan, data.isActive);
                }
                data.loading = false
            },
            //禁用
            del(val: any, isBan = 1) {
                data.loading = true
                BanUsers({
                    ids: [val.id],
                    isBan: isBan
                }).then((res: any) => {
                    console.log("-----------", res);
                    if (res.result === 0) {
                        events.data(data.key, data.isBan, data.isActive);
                        ElMessage({
                            message: '操作成功',
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: '操作失败',
                            type: 'error',
                        })
                    }
                    data.loading = false
                })
            },
            //不看禁用，只看激活
            checkbox(val: any, item: any) {
                data.loading = true
                if (item.title == "只看激活" && val == true) {
                    data.isActive = 1;
                    events.data(data.key, data.isBan, data.isActive);
                } else {
                    data.isActive = "";
                    events.data(data.key, data.isBan, data.isActive);
                }

                if (item.title == "不看禁用" && val == true) {
                    data.isBan = 0;
                    events.data(data.key, data.isBan, data.isActive);
                } else {
                    data.isBan = "";
                    events.data(data.key, data.isBan, data.isActive);
                }
                data.loading = false
            },
            //启用
            des(val: any) {
                events.del(val, 0);
            },

        }
        onMounted(() => {
            events.data(data.key, data.isBan, data.isActive)

        })
        return {
            ...toRefs(data),
            ...events,
        }
    },
})
</script>

<style scoped>
.head {
    border: #ada5a5 solid 2px;
    height: 60px;
    box-shadow: 3px 1px 1px 1px #ccc;
    border-radius: 5px;
    margin-top: 3vh;
    width: 96%;
    margin-left: 3vh;
}

.main {
    margin-top: 3vh;
    box-shadow: 3px 1px 1px 1px #ccc;
    margin-left: 3vh;
}

.table {
    width: 98%;
    border-radius: 10px;
    border: #ada5a5 solid 2px;
}

.header {
    border: #ccc solid 1px;
    margin-top: 2vh;
    background-color: rgba(239, 240, 245, 0.932);
    border-radius: 5px;
}
</style>